////
//// Created by enemy1205 on 2021/8/19.
////
//#Include "Armor.h"
//
//void ArmorNumClassifier::Label(Mat &trainingImages, vector<int> &trainingLabels) {
//    for (int i = 1; i < 6; ++i) {
//        for (int j = 1; j < 4; ++j) {
//            //训练图片存放地址
//            string file_path =
//                    "/media/enemy1205/enemy1205/Template/num_train/" + to_string(i) + "." + to_string(j) + ".png";
//            Mat SrcImage = imread(file_path);
//            SrcImage = SrcImage.reshape(1, 1);
//            trainingImages.push_back(SrcImage);
//            trainingLabels.push_back(i);
//        }
//    }
//}
//
//void ArmorNumClassifier::train() {
//    //获取训练数据
//    Mat classes;
//    Mat trainingData;
//    Mat trainingImages;
//    vector<int> trainingLabels;
//    //getBubble()将获取一张图片后会将图片（特征）写入到容器中，紧接着会将标签写入另一个容器中，这样就保证了特征和标签是一一对应的关系
//    Label(trainingImages, trainingLabels);
//    //在主函数中，将getBubble()与getNoBubble()写好的包含特征的矩阵拷贝给trainingData，将包含标签的vector容器进行类
//    //型转换后拷贝到trainingLabels里，至此，数据准备工作完成，trainingData与trainingLabels就是我们要训练的数据。
//    Mat(trainingImages).copyTo(trainingData);
//    trainingData.convertTo(trainingData, CV_32FC1);
//    Mat(trainingLabels).copyTo(classes);
//    // 创建分类器并设置参数
//    Ptr<SVM> SVM_params = SVM::create();
//    SVM_params->setType(SVM::C_SVC);
//    SVM_params->setKernel(SVM::LINEAR);  //核函数
//    SVM_params->setDegree(0);
//    SVM_params->setGamma(1);
//    SVM_params->setCoef0(0);
//    SVM_params->setC(1);
//    SVM_params->setNu(0);
//    SVM_params->setP(0);
//    SVM_params->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, 0.01));
//    Ptr<TrainData> tData = TrainData::create(trainingData, ROW_SAMPLE, classes);
//    // 训练分类器
//    SVM_params->train(tData);
//    //保存模型
//    SVM_params->save("./svm.xml");//将训练好的模型保存在此文件中。
//    cout << "训练完毕" << endl;
//    getchar();
//}
//
//void ArmorNumClassifier::Predicted(const Mat &img) {
////    string test_path = "";
////Mat test_img = imread(test_path);
//    p = img.reshape(1, 1);
//    p.convertTo(p, CV_32FC1);
//    int response = int(svm->predict(p));
////    cout << "识别的数字为：" << response << endl;
//
//}
//
//ArmorNumClassifier::ArmorNumClassifier() {
//    svm = StatModel::load<SVM>("/home/enemy1205/文档/code/svm.xml");//使用默认模型
//    svm = ml::SVM::create();
//    armorImgSize = Size(100, 100);
//    p = Mat();
//    warpPerspective_mat = Mat(3, 3, CV_32FC1);
//    dstPoints[0] = Point2f(0, 0);
//    dstPoints[1] = Point2f(armorImgSize.width, 0);
//    dstPoints[2] = Point2f(armorImgSize.width, armorImgSize.height);
//    dstPoints[3] = Point2f(0, armorImgSize.height);
//}
//
//void ArmorNumClassifier::loadModel(string &model_path, Size armorImgSize) {
//    svm = StatModel::load<SVM>(model_path);//使用自定义模型
//    if (svm.empty()) {
//        cout << "Svm load error! Please check the path!" << endl;
//        exit(0);
//    }
//    this->armorImgSize = armorImgSize;//使用自定义尺寸
//    dstPoints[0] = Point2f(0, 0);
//    dstPoints[1] = Point2f(armorImgSize.width, 0);
//    dstPoints[2] = Point2f(armorImgSize.width, armorImgSize.height);
//    dstPoints[3] = Point2f(0, armorImgSize.height);
//}
//
//void ArmorNumClassifier::loadImg(const Mat &img) {
//    (img).copyTo(warpPerspective_src);
//    cvtColor(warpPerspective_src, warpPerspective_src, COLOR_BGR2GRAY);
////    threshold(warpPerspective_src, warpPerspective_src, 10, 255, THRESH_BINARY);
//}
//
//void ArmorNumClassifier::getConfrontArmor(Armor_ptr &armor) {
//    srcPoints[0] = Point2f (0,0);
//    srcPoints[1]=armor->getVertexes()[2]-armor->getVertexes()[1];
//    srcPoints[2]=armor->getVertexes()[3]-armor->getVertexes()[1];
//    srcPoints[3]=armor->getVertexes()[0]-armor->getVertexes()[1];
//    //仿射变换得到正视装甲板图
//    warpPerspective_mat = getPerspectiveTransform(srcPoints, dstPoints);  //透射变换矩阵
//    warpPerspective(warpPerspective_src, warpPerspective_dst, warpPerspective_mat, armorImgSize, INTER_NEAREST,
//                    BORDER_CONSTANT, Scalar(0)); //仿射变换
//    warpPerspective_dst.copyTo(armor->ConfrontImg); //获得正视图
//}
//
//void ArmorNumClassifier::getArmorNum(Armor_ptr &armor) {
//    //将正视图转换为SVM模型所接受的格式
//    p = armor->ConfrontImg.reshape(1, 1);
//    p.convertTo(p, CV_32FC1);
//    //获得装甲板号码
//    try {
//        armor->ArmorNum = (int) svm->predict(p);
//    }
//    catch (...) {
//    }
//}